/**
 * 
 */
package pizza.admin.entity;

import static javax.persistence.GenerationType.IDENTITY;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

import pizza.admin.helper.FormatterHelper;
import pizza.admin.helper.HtmlHelper;

/**
 * @author Joro
 * @version 1.0
 */

@Entity
@Table(name = "Orders")
public class Order {
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = true)
	private Integer id;

	@Column(name = "description", unique = false, nullable = false)
	private String description;

	@Column(name = "address", unique = false, nullable = false)
	private String address;

	@Column(name = "phone", unique = false, nullable = false)
	private String phone;

	@Column(name = "date")
	@Type(type = "timestamp")
	private Date date;

	@ManyToOne
	@JoinColumn(name = "userId")
	private User user;
	
	@OneToMany(cascade= CascadeType.REMOVE,mappedBy="order", fetch = FetchType.LAZY)
    private Collection<OrderPizza> orederPizas;
	
	public Order() {
		super();
		id = null;
		description = "";
		address = "";
		phone = "";
		date = new Date(System.currentTimeMillis());
		user = new User();
	}

	

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public Collection<OrderPizza> getOrederPizas() {
		return orederPizas;
	}

	public void setOrederPizas(Collection<OrderPizza> orederPizas) {
		this.orederPizas = orederPizas;
	}
	
	public Double calculateFullPrice(){
		FormatterHelper formatter = new FormatterHelper();
		Double fullPrice = 0.0;
		Collection<OrderPizza> orderedPizas = (Collection<OrderPizza>) this.getOrederPizas();
		
		if (orderedPizas != null && orderedPizas.size() > 0) {
			Iterator orderedPizzasIterator = orderedPizas.iterator();
			while (orderedPizzasIterator.hasNext()) {
				OrderPizza orderedPizza = (OrderPizza) orderedPizzasIterator
						.next();
				fullPrice+= orderedPizza.calculateFullPrice();
			}
		}
		
		return formatter.roundTwoDecimals(fullPrice);
	}
	
	public Boolean validateForDB() {

		// validate before add
		if (address.isEmpty()||phone.isEmpty()||user.getId()==null) {
			return false;
		}
		return true;

	}

	public void purifyFields() {
		HtmlHelper helper = new HtmlHelper();
		address = helper.purify(address);
		phone = helper.purify(phone);
		description = helper.purify(description);

	}

}
